<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!is_registered()) exit;
if (!isset($_POST['blog_name']) || !isset($_POST['value'])) exit;

$id = addslashes($_POST['blog_name']);

$get_com = $db->query ("SELECT cat_rating, cat_voters FROM ".DB_PEREFIX."_art_category WHERE cat_name = '$id'");
if ($db->num_rows($get_com) != 1) exit;

list($karma, $voters) = $db->fetch_row($get_com);

$user_karma = (float) my_karma ();

if ($user_karma < 0 && !is_admin ()) exit;


// Увеличиваем значение кармы
$unit = 1;

if (is_admin ()) $unit = 2;
elseif ($user_karma > 100) $unit = 4;
elseif ($user_karma > 50) $unit = 3;
elseif ($user_karma > 20) $unit = 2;

if ($user_karma > 20 || is_admin ()) $unit = 2;
if ($user_karma > 50) $unit = 3;
if ($user_karma > 100) $unit = 4;

$unit = $unit.'.'.mt_rand(0,50);

if ($_POST['value'] == 1)
	$new = $karma + $unit;
else
	$new = $karma - $unit;
	
$new = number_format($new, 2, '.', '');

// Добавляем пользователя к проголовавшим
if ($voters == '' || $voters == 0)
	$new_voters = User::$properties['user_id'];
else
	$new_voters = $voters.','.User::$properties['user_id'];
	
	
// Не забываем про проверку
$voters = explode (',', $voters);
foreach ($voters as $v) {
	if (trim($v) == User::$properties['user_id']) exit;
}


if ($db->query("UPDATE ".DB_PEREFIX."_art_category SET cat_rating = '$new', cat_voters = '$new_voters' WHERE cat_name = '$id'")) {
	
	Templater::load ('articles/blog_info.tpl');
	$new = number_format($new, 2, ',', '');
	Templater::set_var ('rating', $new);
	
	print Templater::display_part ('blog_info_karma_cant');
}
